package com.lu.business.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lu.business.core.response.ResponseResult;
import com.lu.business.model.Order;
import com.lu.business.model.OrderItem;
import com.lu.business.service.OrderItemService;
import com.lu.business.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @program ShardingsphereDemo
 * @description:
 * @author: zhanglu
 * @create: 2021-08-30 20:25:00
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @GetMapping("/list")
    public ResponseResult list(){
        return ResponseResult.success()
                .params("order", orderService.list())
                .params("order_item", orderItemService.list());
    }

    @GetMapping("/get/{orderId}")
    public ResponseResult get(@PathVariable("orderId") Long orderId){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        return ResponseResult.success()
                .params("info", orderService.orderInfo(orderId));
    }

    @GetMapping("/get2/{orderId}")
    public ResponseResult get2(@PathVariable("orderId") Long orderId){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        return ResponseResult.success()
                .params("order", orderService.getById(orderId))
                .params("order_item", orderItemService.getOne(queryWrapper))
                .params("info", orderService.orderInfo2(orderId))   //绑定表 分片键要相同
                ;
    }

    @GetMapping("/insert")
    public ResponseResult insert(){
        Integer userId = Integer.valueOf(RandomUtil.randomNumbers(8));
        String orderCode = RandomUtil.randomString(12);
        Order order = Order.builder().orderCode(orderCode).userId(userId).build();
        boolean resOrder = orderService.save(order);
        log.info("insert-order-res:" + resOrder + "--->" + order);
        OrderItem orderItem = OrderItem.builder().orderId(order.getId()).orderCode(orderCode).userId(userId).itemId(userId).build();
        boolean resOrderItem = orderItemService.save(orderItem);
        log.info("insert-order_item-res:" + resOrderItem + "--->" + orderItem);
        return ResponseResult.success();
    }

    @Resource
    private ShardingSphereDataSource dataSource;

    @GetMapping("/rules")
    public ResponseResult rules(){
        Map<String, ShardingSphereMetaData> metaDataMap = dataSource.getMetaDataContexts().getMetaDataMap();
        return ResponseResult.success(metaDataMap);
    }

}
